- 
                Notifications
    
You must be signed in to change notification settings  - Fork 45
 
Delete and retry tasks through Django Admin #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| 
               | 
          ||
| 
               | 
          ||
| def retry(modeladmin: "DBTaskResultAdmin", request, queryset): | ||
| rows = queryset.update(status=ResultStatus.NEW) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RealOrangeOne I'm not sure if it's a good idea to reuse DBTaskResult rows; maybe we should be creating new rows instead. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my use case (processing a bunch of documents), I find it useful to be able to retry failed tasks, until there are none left, fixing errors in the process. If we were to create new rows, then I'd have to stop the queue, retry the tasks, delete the old tasks, and then restart the queue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we shall have both! I've updated the PR as you suggest, with a small test to make sure that duplicate tasks actually work.
faae1df    to
    a608fc1      
    Compare
  
    | 
           @RealOrangeOne Any plans of merging this soon? We would need this. :)  | 
    
| 
           @Alwinator it's a draft PR with failing tests and conflicts. It's not as simple as just "merging" it I'm afraid. Once it's ready, it'll absolutely be merged.  | 
    
| 
           Thank you! @mgax Any plans to finish your request? :)  | 
    
| 
           Sure, I'd forgotten about this, I'll see about rebasing and fixing the tests.  | 
    
9125545    to
    dfb443e      
    Compare
  
    | 
               | 
          ||
| object.__setattr__(task_result, "_exception_class", exception_class) | ||
| object.__setattr__(task_result, "_traceback", self.traceback or None) | ||
| object.__setattr__(task_result, "_return_value", self.return_value) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I rebased my changes, I noticed that this assertion was failing:
| assert result_1.return_value == result_2.return_value | 
I'm pretty sure this used to work, and it looks like the database backend is not fetching return values. Did I find a bug? I'm happy to write a specific test for checking that return_value is set on the task result.
| 
           I've rebased the branch. I'm not sure why the CI is failing now. I've narrowed it down to this change: mgax@36d3d38. It looks like the failures are flaky, and I'm guessing that the test somehow causes other tests to fail. But I can't reproduce it locally; for me (MacOS 15.1.1, Python 3.13, Django 5.0)   | 
    
| ] | ||
| ) | ||
| 
               | 
          ||
| def duplicate(self) -> Self: | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: This API feels like one better suited for the TaskResult, rather than being backend specific.
      
        
              This comment has been minimized.
        
        
      
    
  This comment has been minimized.
| 
           Sharing a quick snippet if someone is trying to reenqueue stale  from django_tasks.backends.database.models import DBTaskResult, ResultStatus
DBTaskResult.objects.filter(status=ResultStatus.RUNNING).update(status=ResultStatus.NEW)Update: Bonus management command! # python manage.py reset_running_tasks
from django.core.management.base import BaseCommand
from django_tasks.backends.database.models import DBTaskResult, ResultStatus
class Command(BaseCommand):
    help = 'Resets all DBTaskResult objects with status RUNNING to status NEW.'
    def handle(self, *args, **options):
        updated = DBTaskResult.objects.filter(status=ResultStatus.RUNNING).update(status=ResultStatus.NEW)
        self.stdout.write(self.style.SUCCESS(f'Successfully reset {updated} running tasks to NEW.'))  | 
    

I found it super useful to be able to delete and to reschedule tasks through the Django Admin.
Update – this PR now adds two admin actions:
statusof the selected tasks toNEW, so they will be executed again.NEW, with no result or started/finished timestamps.